<?php

namespace App\Models\Common\Area;

use App\Models\Common\Area\Repositories\Interfaces\AreaRepositoryInterface;
use Eloquent;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Carbon;
use Overtrue\LaravelPinyin\Facades\Pinyin;

/**
 * App\Models\Common\Area\Area
 *
 * @property int         $id
 * @property string      $name        区域名称
 * @property string      $letter      区域名称首字母
 * @property string      $name_uy     区域名称-维语
 * @property string      $letter_uy   区域名称首字母-维语
 * @property string      $adcode      区域编码
 * @property string      $parent_code 上级区域编码
 * @property string      $citycode    城市编码
 * @property string      $level       行政区划级别
 * @property int         $type        类型 1 自治区 2 直辖市 3 省直辖县
 * @property Carbon|null $created_at
 * @property Carbon|null $updated_at
 * @property string      $center      城市中心坐标
 * @method static Builder|Area newModelQuery()
 * @method static Builder|Area newQuery()
 * @method static Builder|Area query()
 * @method static Builder|Area whereAdcode($value)
 * @method static Builder|Area whereCenter($value)
 * @method static Builder|Area whereCitycode($value)
 * @method static Builder|Area whereCreatedAt($value)
 * @method static Builder|Area whereId($value)
 * @method static Builder|Area whereLetter($value)
 * @method static Builder|Area whereLetterUy($value)
 * @method static Builder|Area whereLevel($value)
 * @method static Builder|Area whereName($value)
 * @method static Builder|Area whereNameUy($value)
 * @method static Builder|Area whereParentCode($value)
 * @method static Builder|Area whereType($value)
 * @method static Builder|Area whereUpdatedAt($value)
 * @mixin Eloquent
 */
class Area extends Model
{
    use HasFactory;

    /**
     * country:国家
     *
     * province:省份（直辖市会在province和city显示）
     *
     * city:市（直辖市会在province和city显示）
     *
     * district:区县
     */
    const SUPPORT_LEVELS
                         = [
            'country',
            'province',
            'city',
            'district',
        ];
    const COUNTRY_LEVEL  = 1;
    const PROVINCE_LEVEL = 2;
    const CITY_LEVEL     = 4;
    const DISTRICT_LEVEL = 8;

    protected $table = 'area';
    protected $fillable
                     = [
            'name',
            'parent_code',
            'level',
            'letter',
            'adcode',
            'citycode',
            'name_uy',
            'letter_uy',
            'type',
            'center',
        ];

    public function getAreaNameUy()
    {
        return $this->getAttribute('name_uy');
    }

    public function getLetter()
    {
        return $this->getAttribute('letter');
    }

    public function getLetterUy()
    {
        return $this->getAttribute('letter_uy');
    }

    public function getAdCode()
    {
        return $this->getAttribute('adcode');
    }

    public function getCityCode()
    {
        return $this->getAttribute('citycode');
    }

    public function getParent(): ?self
    {
        $areaRepo = app(AreaRepositoryInterface::class);
        return $areaRepo->findByCode($this->getParentCode());
    }

    public function getParentCode()
    {
        return $this->getAttribute('parent_code');
    }

    /**
     * 是否是直辖市
     * @return bool
     */
    public function isMunicipality(): bool
    {
        return $this->type == 2;
    }

    /**
     * 是否是区
     *
     * @return bool
     */
    public function isDistrict(): bool
    {
        return $this->level == 'district';
    }

    public function getAreaName()
    {
        return $this->getAttribute('name');
    }

    protected function pinYin(): Attribute
    {
        return Attribute::make(
            get: fn($value) => ucwords(Pinyin::phrase($this->name, ' ')),
        )->shouldCache();
    }
}
